package com.zhang.modules.cos;

import com.tencent.cloud.Response;
import com.zhang.CosService;
import com.zhang.common.web.respWrapper.Res;
import com.zhang.exception.exception.NormalException;
import com.zhang.modules.api.annotation.ApiPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@Api(tags = "对象存储管理")
@RequestMapping("/api/cos")
@ApiPermission("sys:cos:*")
public class CosController {

    private CosService service;

    public CosController(CosService service) {
        this.service = service;
    }

    @ApiOperation("获取临时上传凭证")
    @ApiPermission("sys:cos:tempCredential")
    @GetMapping("/tempCredential")
    public Res<Response> getTempCredential(){
        try {
            Response Response = service.getTempCredential();
            return Res.ok(Response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new NormalException("获取临时上传凭证失败");
        }
    }

    @ApiOperation("获取临时上传URL")
    @ApiPermission("sys:cos:tempUploaDUrl")
    @GetMapping("/tempUploadUrl")
    public Res<String> getTempUploadUrl(String objectName){
        try {
            String url = service.getTempUploadUrl(objectName);
            return Res.ok(url);
        } catch (Exception e) {
            e.printStackTrace();
            throw new NormalException("获取临时上传路径失败");
        }
    }

    @ApiOperation("获取对象访问URL")
    @ApiPermission("sys:cos:objectUrl")
    @GetMapping("/objectUrl")
    public Res<String> getTempUploadUrl(String keyName,String bucketName){
        try {
            String url = service.getObjectUrl(keyName,bucketName);
            return Res.ok(url);
        } catch (Exception e) {
            e.printStackTrace();
            throw new NormalException("获取对象访问路径失败");
        }
    }
}
